package model

import (
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
	"time"
)

//BaseModel base model
type BaseModel struct {
	ID        int       `gorm:"ForeignKey:ID" gorm:"primary_key" form:"id" json:"id"` //主键ID
	CreatedAt time.Time `json:"created_at" form:"-"`                                  //记录创建时间
	UpdatedAt time.Time `json:"updated_at" form:"-"`                                  //记录更新时间
}

type model struct {
	DB *gdb.Model
}

var Model = new(model)

func Table(name string) *model {
	return &model{DB: g.DB("default").Model(name)}
}

// when callback
func (m *model) When(value bool, data interface{}, f func(q *gdb.Model, data interface{}) (*gdb.Model)) *model {
	if (value) {
		m.DB = f(m.DB, data)
		return m
	}
	return m
}

// get model
func (m *model) Query() *gdb.Model {
	return m.DB
}
